package cn.yhbk.web.modules.manager.Controller;

import cn.hutool.core.bean.BeanUtil;
import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.DTO.Notice.TbNoticeDto;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbNotice;
import cn.yhbk.web.modules.manager.Pojo.VO.Notice.NoticeAdd;
import cn.yhbk.web.modules.manager.Pojo.VO.Notice.NoticePag;
import cn.yhbk.web.modules.manager.Pojo.VO.Notice.NoticeUpdate;
import cn.yhbk.web.modules.manager.Service.ITbNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@Api(tags = "公告接口")
@Slf4j
@RestController
@RequestMapping("/manager/tbNotice")
public class TbNoticeController {

    @Autowired
    private ITbNoticeService iTbNoticeService;

    @ApiOperation("根据ID查询公告")
    @GetMapping("/getNoticeInfo")
    public CommonResult<TbNoticeDto> getNoticeInfo(@RequestParam("id") Long id){
        log.info("查询公告,id:" + id);
        // TODO 这里查询有点问题
        return CommonResult.success(iTbNoticeService.queryNoticeInfoById(id));
    }

    @ApiOperation("增加公告")
    @PostMapping("/saveNotice")
    public CommonResult<String> saveNotice(@RequestBody NoticeAdd noticeAdd){
        log.info("增加公告，" + noticeAdd);
        Long id = MyUtil.getCurrentAdminId();
        TbNotice tbNotice = BeanUtil.copyProperties(noticeAdd, TbNotice.class);
        tbNotice.setCreateTime(LocalDateTime.now());
        tbNotice.setUpdateTime(LocalDateTime.now());
        tbNotice.setReleaserId(id);
        tbNotice.setPageView(0L);
        tbNotice.setStatus(1);
        Boolean saveNotice = iTbNoticeService.save(tbNotice);
        if(!saveNotice){
            return CommonResult.failed("保存公告出错");
        }
        return CommonResult.success("保存成功");
    }

    @ApiOperation("修改公告")
    @PutMapping("/updateNotice")
    public CommonResult<String> updateClass(@RequestBody NoticeUpdate noticeUpdate){
        log.info("修改公告，" + noticeUpdate);
        TbNotice tbNotice = iTbNoticeService.getById(noticeUpdate.getId());
        BeanUtils.copyProperties(noticeUpdate, tbNotice);
        tbNotice.setUpdateTime(LocalDateTime.now());
        Boolean updateNotice = iTbNoticeService.updateById(tbNotice);
        if(!updateNotice){
            return CommonResult.failed("修改公告出错");
        }
        return CommonResult.success("修改成功");
    }

    @ApiOperation("删除公告")
    @PostMapping("/deleteNotice/{noticeId}")
    public CommonResult<String> deleteClass(@PathVariable("noticeId") Long noticeId){
        log.info("删除公告，id:" + noticeId);
        Boolean deleteNotice = iTbNoticeService.removeById(noticeId);
        if(!deleteNotice){
            return CommonResult.failed("删除公告出错");
        }
        return CommonResult.success("删除成功");
    }

    @ApiOperation("分页获取公告")
    @GetMapping("/getNoticeInfoPag")
    public CommonResult<NoticePag> getNoticeInfoPag(@RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                                   @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize){
        NoticePag noticePag = iTbNoticeService.getNoticeInfoPag(currentPage, pageSize);
        return CommonResult.success(noticePag);
    }
}
